<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:xunjian'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起巡检') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectno }}
        </a>
        
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjXjPmInspectionList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted,watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmInspectionDelete, pmInspectionListData } from '/@/api/proj/xj/pmInspection';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.xj.pmInspection');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('巡检管理'),
    value: t('巡检管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编号'),
        field: 'projectno',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('项目名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('巡检日期'),
        field: 'inspectiondate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('巡检人'),
        field: 'inspector',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 250,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('巡检日期'),
      dataIndex: 'inspectiondate',
      key: 'a.inspectiondate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('巡检结果'),
      dataIndex: 'checkresult',
      key: 'a.checkresult',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('巡检人'),
      dataIndex: 'inspector',
      key: 'a.inspector',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('巡检备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 150,
      align: 'left',
      ifShow: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 60,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑巡检'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:edit:xunjian',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除巡检'),
        popConfirm: {
          title: t('是否确认删除巡检'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:xunjian',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmInspectionListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmInspectionDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: props.projectno?props.projectno:userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: props.projectno?props.projectno:userStore.getProjecte.code,
    });
  }
</script>
